package com.wj.oauth2.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;

@Configuration
public class TokenConfig {

/*    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    @Bean
    public TokenStore tokenStore(){
        return new RedisTokenStore(redisConnectionFactory);
    }*/

    @Bean
    public TokenStore tokenStore(){
        //return new JdbcTokenStore(dataSource);
        //jwt管理令牌
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    // JWT 签名秘钥
   // private static final String SIGNING_KEY = "wj-key";

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
//        jwtAccessTokenConverter.setSigningKey(SIGNING_KEY);
//        return jwtAccessTokenConverter;
        KeyStoreKeyFactory keyFactory = new KeyStoreKeyFactory(new ClassPathResource("oauth2.jks"), "oauth2".toCharArray());
        jwtAccessTokenConverter.setKeyPair(keyFactory.getKeyPair("oauth2"));
        return jwtAccessTokenConverter;
    }

}
